﻿<%@ Application Language="VB" %>

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
        ' Code that runs on application startup
        Application.Lock()
        Application("StartTime") = Now.ToLongDateString
        Application("SessionCount") = 0
        Application("RequestCount") = 0
        Application.UnLock()

        'Load the Items into an array
        'Dim objItem As clsItem = New clsItem()
        'Dim aItemList As ArrayList
        'aItemList = objItem.Retrieve()
        'Application.Add("ItemList", aItemList)
        'objItem = Nothing

    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started
        ' Code that runs when a new session is started
        Dim lngCount As Long
        Application.Lock()
        lngCount = CLng(Application.Get("SessionCount").ToString) + 1
        Application("SessionCount") = lngCount
        Application.UnLock()
    End Sub
    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
        ' Code that runs when a session ends. 
        ' Note: The Session_End event is raised only when the sessionstate mode
        ' is set to InProc in the Web.config file. If session mode is set to StateServer 
        ' or SQLServer, the event is not raised.
        Dim lngCount As Long
        Application.Lock()
        lngCount = CLng(Application.Get("SessionCount").ToString) - 1
        Application("SessionCount") = lngCount
        Application.UnLock()
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires at the beginning of each request
        ' ----------------------------------------------
        Application.Lock()
        Application("RequestCount") = CLng(Application("RequestCount").ToString) + 1
        Application.UnLock()
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
        'Code that runs when an unhandled error occurs
        'Get exception details
        Dim ex As Exception = HttpContext.Current.Server.GetLastError()

        Dim strError As String = ex.Message

        If TypeOf ex Is HttpUnhandledException AndAlso ex.InnerException IsNot Nothing Then
            ex = ex.InnerException
        End If

        If ex IsNot Nothing Then
            Dim bDebug As Boolean = False
            If ConfigurationManager.AppSettings("LogAppErrors").ToString().Equals("False") Then
                bDebug = False
            Else
                bDebug = True
            End If

            Try
                'Email the administrator with information that an error has occurred
                'Use this section if elmah is not able to send emails
                '---------------------------------------------------------
                'Dim FromAddress As String = ConfigurationManager.AppSettings("ErrorMailFromAccount").ToString()
                'Dim ToAddress As String = ConfigurationManager.AppSettings("ErrorMailToAccount").ToString()

                ''(1) Create the MailMessage instance
                'Dim mm As New System.Net.Mail.MailMessage(FromAddress, ToAddress)

                ''(2) Assign the MailMessage's properties
                'With mm
                '    .Subject = "An unhandled exception occurred!"
                '    .Body = String.Format("An unhandled exception occurred:{0}Message: {1}{0}{0} Stack Trace:{0}{2}", System.Environment.NewLine, ex.Message, ex.StackTrace)
                '    .IsBodyHtml = False
                'End With

                ''(3) Create the SmtpClient object
                'Dim smtp As New System.Net.Mail.SmtpClient

                ''(4) Send the MailMessage (will use the Web.config settings)
                'smtp.Send(mm)
                '---------------------------------------------------------            
                Server.Transfer("~/CustomErrorPage.aspx")

            Catch smtpEx As System.Net.Mail.SmtpException
                'A problem occurred when sending the email message
                If bDebug Then
                    Throw (smtpEx) 'debugging 
                Else
                    'production
                    Server.Transfer("~/CustomErrorPage.aspx")
                End If

            Catch generalEx As Exception
                'Some other problem occurred
                If bDebug Then
                    Throw (generalEx) 'debugging
                Else
                    'production
                    Server.Transfer("~/CustomErrorPage.aspx")
                End If

            End Try
        End If
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub
       
</script>